package com.wjw.springsecuritydemo.handler;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;

import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
//前后端分离模式不能用controller跳转
public class MyAuthenticationSuccessHandler implements AuthenticationSuccessHandler {
    private String url;

    public MyAuthenticationSuccessHandler(String url){
        this.url=url;
    }

    @Override
    public void onAuthenticationSuccess(
                HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse,
                Authentication authentication)
            throws IOException, ServletException {
        System.out.println("登录ip"+httpServletRequest.getRemoteAddr());
        //user对象
        //Object principal=authentication.getPrincipal();
        User user=(User)authentication.getPrincipal();
        //密码固定为null （安全
        System.out.println("用户："+user.getUsername()+"----------权限"+user.getAuthorities());
        httpServletResponse.sendRedirect(url);
    }
}
